package com.zjw.shortlink.admin.controller;

import com.zjw.shortlink.admin.common.convention.result.Result;
import com.zjw.shortlink.admin.common.convention.result.Results;
import com.zjw.shortlink.admin.dto.req.UserLoginReqDTO;
import com.zjw.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.zjw.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.zjw.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.zjw.shortlink.admin.dto.resp.UserRealRespDTO;
import com.zjw.shortlink.admin.dto.resp.UserRespDTO;
import com.zjw.shortlink.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> isExistUsername(@RequestParam("username") String username) {
        return Results.success(userService.isExistUsername(username));
    }

    @PostMapping("/api/short-link/admin/v1/user/get-user")
    public Result<UserRespDTO> getUser(@RequestParam("username") String username) {
        return Results.success(userService.getUserByUserName(username));
    }

    @PostMapping("/api/short-link/admin/v1/user/get-real-user")
    public Result<UserRealRespDTO> getRealUser(@RequestParam("username") String username) {
        return Results.success(userService.getRealUserByUserName(username));
    }

    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam) {
        userService.register(requestParam);
        return Results.success();
    }

    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestParam) {
        userService.update(requestParam);
        return Results.success();
    }

    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam) {
        UserLoginRespDTO token = userService.login(requestParam);
        return Results.success(token);
    }

    @PostMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam String username, @RequestParam String token) {
        Boolean flag = userService.checkLogin(username, token);
        return Results.success(flag);
    }

    @PostMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam String username, @RequestParam String token) {
        userService.logout(username, token);
        return Results.success();
    }
}
